package br.com.reparoja.entities;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name = "equipment")
public class Equipment {

	public Equipment() {
		this.brands = new ArrayList<Brand>();
	}

	public Equipment(String name) {
		this();

		this.name = name;
	}
	
	@Id
	@Column(unique=true)
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@Column
	private String name;

	@ManyToMany(targetEntity = Brand.class, cascade = CascadeType.ALL, mappedBy="equipments")
	private List<Brand> brands;

	public Long getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Brand> getBrands() {
		return brands;
	}

	public void setBrands(List<Brand> brands) {
		this.brands = brands;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("{");
		sb.append("\"id\": ").append(this.id).append(", ");
		sb.append("\"name\": \"").append(this.name).append("\"");

		if (this.brands.size() > 0) {
			sb.append(", \"brands\": [");
			
			for (Brand brand : this.brands) {
				sb.append(brand.toString()).append(", ");
			}

			sb.setLength(sb.length() - 2);
			sb.append("]");
		}
		
		sb.append("}");
		
		return sb.toString();
	}

}
